package com.elm.orders.service.impl;

import com.elm.api.client.BusinessClient;
import com.elm.api.client.FoodClient;
import com.elm.api.client.OrderDetailetClient;
import com.elm.common.pojo.OrderDetailet;
import com.elm.common.pojo.Result;
import com.elm.orders.mapper.OrdersMapper;
import com.elm.orders.pojo.Orders;
import com.elm.orders.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elm.common.pojo.Business;
import com.elm.common.pojo.Food;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderDetailetClient orderDetailetClient;
    @Autowired
    private BusinessClient businessClient;
    @Autowired
    private FoodClient foodClient;

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders=ordersMapper.getOrdersById(orderId);
        Integer businessId=orders.getBusinessId();
        List<OrderDetailet> list=orderDetailetClient.getByOrderId(orderId).getData();
        orders.setList(list);
        orders.setBusiness(businessClient.getBusinessById(businessId).getData());
        for (int i = 0; i < list.size(); i++) {
            OrderDetailet orderDetailet=list.get(i);
            Integer foodId=orderDetailet.getFoodId();
            orderDetailet.setFood(foodClient.findFoodByFoodId(foodId).getData());
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        List<Orders> list=ordersMapper.listOrdersByUserId(userId);
        int len=list.size();
        for (int i = 0; i < len; i++) {
            Orders o=list.get(i);
            Integer businessId=o.getBusinessId();
            Business business=businessClient.getBusinessById(businessId).getData();
            Integer orderId=o.getOrderId();
            List<OrderDetailet> orderDetailetList=orderDetailetClient.getByOrderId(orderId).getData();
            for (int j = 0; j < orderDetailetList.size(); j++) {
                OrderDetailet orderDetailet=orderDetailetList.get(j);
                Integer foodId = orderDetailet.getFoodId();
                orderDetailet.setFood(foodClient.findFoodByFoodId(foodId).getData());
            }
            o.setBusiness(business);
            o.setList(orderDetailetList);
        }

        return list;
    }

    @Override
    public void update(Integer orderId) {
        ordersMapper.update(orderId);
    }
    @Override
    public Integer createOrders(Orders orders) {
        ordersMapper.createOrders(orders);
        Integer id=orders.getOrderId();
//        List<Cart> list=cartMapper.listCart(orders.getUserId(),orders.getBusinessId());
//        int len=list.size();
//        for (int i = 0; i < len; i++) {
//            OrderDetailet orderDetailet=new OrderDetailet();
//            Integer foodId=list.get(i).getFoodId();
//            Integer quantity=list.get(i).getQuantity();
//            orderDetailet.setOrderId(id);
//            orderDetailet.setQuantity(quantity);
//            orderDetailet.setFoodId(foodId);
//            orderDetailetMapper.saveOrderDetailet(orderDetailet);
//        }
//        cartMapper.removeCart(orders.getUserId(),orders.getBusinessId(),null);
        return id;
    }
}
